package com.apache.info.service.impl;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.info.entity.PubInfoStatistics;
import com.apache.info.manager.PubInfoStatisticsManager;

import java.util.List;

public class PubInfoStatisticsManagerImpl implements PubInfoStatisticsManager {

	protected IDao pubInfoStatisticsDao;
	protected final String entityName = "com.apache.info.entity.PubInfoStatistics";

	public void setPubInfoStatisticsDao(IDao pubInfoStatisticsDao) {
		this.pubInfoStatisticsDao = pubInfoStatisticsDao;
	}

	public String saveInfo(ParamsVo<PubInfoStatistics> vo) throws BusinessException {
		PubInfoStatistics pubSortInfo = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		pubSortInfo.setInfoId(infoId);
		MethodParam param = new MethodParam("PubInfoStatistics", cacheKey, "", entityName);
		param.setVaule(pubSortInfo);
		if (pubInfoStatisticsDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<PubInfoStatistics> vo) throws BusinessException {
		//		PubSortInfo pubSortInfo = vo.getObj();
		//		if (Validator.isNotNull(pubSortInfo.getSortId())) {
		//			String cacheKey = "pubSortInfo_" + pubSortInfo.getSortId();
		//			MethodParam param = new MethodParam("PubSortInfo", cacheKey, "", entityName);
		//			param.setVaule(pubSortInfo);
		//			return pubSortInfoDao.edit(param);
		//		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<PubInfoStatistics> vo) throws BusinessException {
		PubInfoStatistics pubSortInfo = vo.getObj();
		if (null == pubSortInfo || Validator.isNull(pubSortInfo.getSortId())
				|| Validator.isNull(pubSortInfo.getObjId())) {
			return false;
		}
		String cacheKey = "";
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setParams("sortId", pubSortInfo.getSortId());
		param.setParams("objId", pubSortInfo.getObjId());
		param.setDelete(true);
		return pubInfoStatisticsDao.delete(param);
	}

	public Object getInfoById(ParamsVo<PubInfoStatistics> vo) {
		PubInfoStatistics pubSortInfo = vo.getObj();
		if (null == pubSortInfo || Validator.isNull(pubSortInfo.getSortId())
				|| Validator.isNull(pubSortInfo.getObjId())) {
			return false;
		}
		String cacheKey = "";
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setParams("sortId", pubSortInfo.getSortId());
		param.setParams("objId", pubSortInfo.getObjId());
		return pubInfoStatisticsDao.selectById(param);
	}

	public Object execute(ParamsVo<PubInfoStatistics> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<PubInfoStatistics> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		Page page = pubInfoStatisticsDao.pageSelect(param);
		return page;
	}

	public List<PubInfoStatistics> getList(ParamsVo<PubInfoStatistics> vo) {
		MethodParam param = setMethodParams(vo, 2);
		return pubInfoStatisticsDao.select(param);
	}

	public long countInfo(ParamsVo<PubInfoStatistics> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return pubInfoStatisticsDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<PubInfoStatistics> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		PubInfoStatistics pubSortInfo = vo.getObj();
		if (Validator.isEmpty(pubSortInfo))
			return param;
		if (Validator.isNotNull(pubSortInfo.getSortId())) {
			param.setParams("sortId", pubSortInfo.getSortId());
		}
		if (Validator.isNotNull(pubSortInfo.getObjId())) {
			param.setParams("objId", pubSortInfo.getObjId());
		}
		if (Validator.isNotNull(pubSortInfo.getInfoUser())) {
			param.setParams("infoUser", pubSortInfo.getInfoUser());
		}
		if (Validator.isNotNull(pubSortInfo.getReserved1())) {
			param.setParams("reserved1", pubSortInfo.getReserved1());
		}
		if (Validator.isNotNull(pubSortInfo.getReserved2())) {
			param.setParams("reserved2", pubSortInfo.getReserved2());
		}
		return param;
	}
}
